package cn.loan.service.impl;

import cn.loan.dao.AuthenticationMapper;
import cn.loan.entity.Authentication;
import cn.loan.service.AuthenticationService;
import cn.loan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: 毛苏宇
 * @Description: You can do it!
 * @Date: Create in 14:14 2019/3/21
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AuthenticationServiceImpl implements AuthenticationService {

    @Autowired
    private AuthenticationMapper authenticationMapper;
    @Autowired
    private UserService userService;

    @Override
    public boolean addAuth(Authentication authentication) {
        //查询邀请码是否存在,不存在则直接为null
        if (userService.findCode(authentication.getUser_code())==false){
            authentication.setUser_code(null);
        }
        return authenticationMapper.insertAuthentication(authentication) == 1 ? true : false;
    }

    @Override
    public boolean authIsExist(int userId) {
        return authenticationMapper.findAuthenticationCountByUserId(userId)==1?true:false;
    }

    @Override
    public Authentication getBankNumOrName(int userId) {
        return authenticationMapper.findBankNumOrNameByUserId(userId);
    }

    @Override
    public boolean getUserCodeExist(int userId) {
        return authenticationMapper.findUserCodeById(userId)==null?false:true;
    }

}
